from tortoise import fields
from base.base_models import BaseModel
from typing import Dict, List, Any


class Menu(BaseModel):
    title = fields.CharField(max_length=50, description="菜单标题")
    icon = fields.CharField(max_length=50, null=True, description="菜单图标")
    path = fields.CharField(max_length=100, null=True, description="菜单路径")
    parent: fields.ForeignKeyNullableRelation["Menu"] = fields.ForeignKeyField(
        "models.Menu", related_name="children", null=True, on_delete=fields.CASCADE
    )
    is_external = fields.IntField(default=0, description="是否外链")
    sort = fields.IntField(default=0, description="排序")
    children: fields.ReverseRelation["Menu"]

    class Meta:
        table = "tb_system_menus"

    def __str__(self):
        return self.title

    async def to_dict(self, include_children=True) -> Dict[str, Any]:
        result = {
            "id": str(self.id),
            "title": self.title,
            "icon": self.icon,
            "path": self.path,
            "parent_id": str(self.parent_id) if self.parent_id else None,
            "is_external": bool(self.is_external),
            "sort": self.sort,
            "active": bool(self.active),
            "created_at": self.created_at,
            "updated_at": self.updated_at,
        }

        if include_children:
            children = await self.children.all()
            if children:
                result["children"] = [await child.to_dict() for child in children]

        return result

    @classmethod
    async def get_tree(cls, **kwargs) -> List[Dict[str, Any]]:
        root_menus = (
            await cls.filter(parent_id__isnull=True, **kwargs)
            .prefetch_related("children")
            .order_by("sort")
        )
        return [await menu.to_dict() for menu in root_menus]


class GlobalConfig(BaseModel):
    title = fields.CharField(max_length=50, description="网站标题")
    description = fields.CharField(max_length=255, description="网站描述", null=True)
    keywords = fields.CharField(max_length=255, description="网站关键词", null=True)
    logo_text = fields.CharField(max_length=50, description="网站 Logo 文字")
    icp = fields.CharField(max_length=50, description="ICP 备案号", null=True)
    sider_name = fields.CharField(max_length=50, description="侧边栏名称")
    sider_avatar = fields.CharField(max_length=255, description="侧边栏头像")
    sider_description = fields.CharField(max_length=255, description="侧边栏描述")
    sider_cover = fields.CharField(max_length=255, description="侧边栏背景")
    github_url = fields.CharField(max_length=255, description="GitHub 地址", null=True)
    email = fields.CharField(max_length=255, description="邮箱", null=True)
    tag_cloud = fields.BooleanField(default=True, description="是否显示标签云")
    recent_posts = fields.BooleanField(default=True, description="是否显示最新文章")
    time_line = fields.BooleanField(default=True, description="是否显示时间线")
    index_title = fields.CharField(max_length=50, description="首页标题")
    index_subtitle = fields.CharField(max_length=100, description="首页副标题")
    index_cover = fields.CharField(max_length=255, description="首页封面")
    index_blurhash = fields.CharField(max_length=100, description="首页封面的 blurhash")
    category_cover = fields.CharField(max_length=255, description="分类封面")
    category_blurhash = fields.CharField(
        max_length=100, description="分类封面的 blurhash"
    )
    tag_cover = fields.CharField(max_length=255, description="标签封面")
    tag_blurhash = fields.CharField(max_length=100, description="标签封面的 blurhash")
    archive_cover = fields.CharField(max_length=255, description="归档封面")
    archive_blurhash = fields.CharField(
        max_length=100, description="归档封面的 blurhash"
    )
    thought_cover = fields.CharField(max_length=255, description="随笔封面")
    thought_blurhash = fields.CharField(
        max_length=100, description="随笔封面的 blurhash"
    )
    message_cover = fields.CharField(max_length=255, description="留言板封面")
    message_blurhash = fields.CharField(
        max_length=100, description="留言板封面的 blurhash"
    )

    class Meta:
        table = "tb_system_global_config"


class Visitor(BaseModel):
    ip = fields.CharField(max_length=255, description="IP 地址", null=True)
    country = fields.CharField(max_length=255, description="国家", null=True)
    region = fields.CharField(max_length=255, description="地区", null=True)
    city = fields.CharField(max_length=255, description="城市", null=True)
    count = fields.IntField(default=0, description="访问次数")

    class Meta:
        table = "tb_system_visitors"
